<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:pbs='http://schema.pbs.gov.au'
  xmlns:rwt='http://schema.pbs.gov.au/RWT/'
  xmlns:db="http://docbook.org/ns/docbook">

  <db:article>
    <db:info>
      <db:title>Expand PBS XML</db:title>

      <db:copyright>
        <db:year>2011</db:year>
        <db:year>2009</db:year>
        <db:holder>Commonwealth of Australia</db:holder>
      </db:copyright>

      <db:legalnotice>
        <db:para>This software is copyrighted by the Commonwealth of Australia ("The Commonwealth").  The following terms apply to all files associated with the software unless explicitly disclaimed in individual files.</db:para>
        <db:para>Permission is hereby granted to use, copy, modify, distribute, and license this software and its documentation for any purpose, provided that existing copyright notices are retained in all copies and that this notice is included verbatim in any distributions. No written agreement, license, or royalty fee is required for any of the authorized uses. Modifications to this software may be copyrighted by their authors and need not follow the licensing terms described here, provided that the new terms are clearly indicated on the first page of each file where they apply.</db:para>
        <db:para>IN NO EVENT SHALL THE COMMONWEALTH OR DISTRIBUTORS BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</db:para>
        <db:para>THE COMMONWEALTH SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE COMMONWEALTH HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.</db:para>
      </db:legalnotice>

      <db:author>
        <db:personname>Steve Ball</db:personname>
        <db:email>Steve.Ball@health.gov.au</db:email>
        <db:affiliation>
          <db:orgname>Department of Health and Ageing</db:orgname>
          <db:orgdiv>PharmBiz</db:orgdiv>
        </db:affiliation>
      </db:author>

      <db:revhistory>
        <db:revision>
          <db:revnumber>2.0</db:revnumber>
          <db:date>2011-01-21</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Updated for PBS XML version 2.4.</db:revremark>
        </db:revision>
        <db:revision>
          <db:revnumber>1.0</db:revnumber>
          <db:date>2009-10-13</db:date>
          <db:authorinitials>SRB</db:authorinitials>
          <db:revremark>Initial version.</db:revremark>
        </db:revision>
      </db:revhistory>
    </db:info>

    <db:para>This XSL stylesheet expands a PBS XML document by resolving all of the internal hyperlinks. This results in a much larger document with duplicate information, but that is more straight-forward to process.</db:para>
    <db:para>The source document is a PBS XML document.</db:para>
    <db:para>The result is an expanded version of the PBS XML document.</db:para>
  </db:article>

  <xsl:output method="xml" indent='yes'/>
  <xsl:strip-space elements='*'/>

  <xsl:key name='ident'
    match='*'
    use='@xml:id|@rdf:about'/>

  <xsl:template match="*">
    <xsl:choose>
      <xsl:when test='contains(name(), "-reference")'>
        <xsl:variable name='target'
          select='key("ident", substring-after(@xlink:href, "#"))'/>

        <xsl:choose>
          <xsl:when test='$target'>
            <xsl:apply-templates select='$target'/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:message>unable to resolve element "<xsl:value-of select='name()'/>" with id "<xsl:value-of select='@xlink:href'/>"</xsl:message>
            <Error>
              <message>
                <xsl:text>unable to resolve element "</xsl:text>
                <xsl:value-of select='name()'/>
                <xsl:text>" with id "</xsl:text>
                <xsl:value-of select='@xlink:href'/>
                <xsl:text>"</xsl:text>
              </message>
            </Error>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
        <xsl:copy>
          <xsl:apply-templates select='@*|node()'/>
        </xsl:copy>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match='pbs:mpp-reference'>
    <xsl:variable name='target'
      select='key("ident", substring-after(@xlink:href, "#"))'/>

    <xsl:choose>
      <xsl:when test='$target'>
        <xsl:for-each select='$target'>
          <xsl:copy>
            <xsl:apply-templates select='@*'/>
            <xsl:for-each select='..'>
              <xsl:copy>
                <xsl:apply-templates select='db:title'/>
              </xsl:copy>
            </xsl:for-each>
            <xsl:apply-templates select='node()'/>
          </xsl:copy>
        </xsl:for-each>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message>unable to resolve element "<xsl:value-of select='name()'/>" with id "<xsl:value-of select='@xlink:href'/>"</xsl:message>
        <Error>
          <message>
            <xsl:text>unable to resolve element "</xsl:text>
            <xsl:value-of select='name()'/>
            <xsl:text>" with id "</xsl:text>
            <xsl:value-of select='@xlink:href'/>
            <xsl:text>"</xsl:text>
          </message>
        </Error>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match='pbs:member-of'>
    <xsl:variable name='group'
      select='key("ident", substring-after(@xlink:href, "#"))'/>

    <xsl:choose>
      <xsl:when test='$group'>
        <xsl:apply-templates select='$group'/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:message>unable to resolve group with id "<xsl:value-of select='@xlink:href'/>"</xsl:message>
        <Error>
          <message>
            <xsl:text>unable to resolve group with id "</xsl:text>
            <xsl:value-of select='@xlink:href'/>
            <xsl:text>"</xsl:text>
          </message>
        </Error>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match='pbs:type |
                       rwt:authority-method'>
    <xsl:copy>
      <xsl:apply-templates select='@*'/>

      <xsl:choose>
        <xsl:when test='contains(., ":")'>
          <xsl:variable name='prefix' select='substring-before(., ":")'/>
          <xsl:choose>
            <xsl:when test='$prefix = "http" or
                            $prefix = "urn"'>
              <xsl:apply-templates/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select='namespace::*[name() = $prefix]'/>
              <xsl:value-of select='substring-after(., ":")'/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:copy>
  </xsl:template>

  <xsl:template match='rwt:restriction-references-list'>
    <rwt:restrictions-list>
      <xsl:apply-templates/>
    </rwt:restrictions-list>
  </xsl:template>

  <xsl:template match='@*'>
    <xsl:copy/>
  </xsl:template>
  <!-- Translate @xml:id into @pbs:id because these will no longer be unique -->
  <xsl:template match='@xml:id'>
    <xsl:attribute name='pbs:id'>
      <xsl:value-of select='.'/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>
